<?php
/**
 * Created by PhpStorm.
 * User: zhang
 * Date: 2018/5/5
 * Time: 11:13
 */

namespace app\shopping\controller;

use app\shopping\model\Commdetailstool;

class Shoppingcarcomm extends Commdetailstool{
    public function __construct()
    {
        parent::__construct();
    }

    public function index(){

        $action = $_GET["action"];
        $uid = $_GET["uid"];

        if ($action == 'get') {
            $this->selectShoppingCart($uid);
        } else {
            $comm_id = $_GET["comm_id"];
            if (!empty($comm_id)) {
                if ($action == 'join') {
                    $comm_sort = $_GET["comm_sort"];
                    if (empty($comm_sort) || $comm_sort == "undefined") {
                        echo echoJson(0, "操作失败，参数为空，-4！");
                    } else {
                        $this->joinShoppingCart($uid, $comm_id, $comm_sort);
                    }
                } else if ($action == 'cancel') {
                    $this->cancelShoppingCart($uid, $comm_id);
                } else if ($action == 'editAttribute') {
                    $comm_sort = $_GET["comm_sort"];
                    if (empty($comm_sort) || $comm_sort == "undefined") {
                        echo echoJson(0, "操作失败,，参数为空，-5！");
                    } else {
                        $this->editAttribute($uid, $comm_id, $comm_sort);
                    }
                }
            } else {
                echo echoJson(0, "操作失败，商品id为空！");
            }
        }
    }


    /**
     *  加入用户的购物车
     * @param str $uid 账号
     */
    function joinShoppingCart($uid, $comm_id, $comm_sort) {
        $comm_quantity = $_GET["comm_quantity"];
        isset($comm_quantity) ? "" : $comm_quantity = 1;
        $selected_attribute = $_GET["selected_attribute"];
        isset($selected_attribute) ? '' : $selected_attribute = null;
        is_array($selected_attribute) ? $selected_attribute = json_encode($selected_attribute, JSON_UNESCAPED_UNICODE) : '';
        $sql = "INSERT INTO sopp_shoppingCart (uid,comm_id,comm_sort,comm_quantity,selected_attribute,datetime) VALUES "
            . "('$uid','$comm_id','$comm_sort',$comm_quantity,'$selected_attribute',now()) "
            . "ON DUPLICATE KEY UPDATE  uid='$uid',comm_id='$comm_id',comm_sort='$comm_sort',"
            . "selected_attribute='$selected_attribute',comm_quantity=$comm_quantity,datetime=now()";
        if ($this->mysql->insertSQL($sql)) {
            echo echoJson(1, $this->getshoppingCartCommNumb($uid));
        } else {
            echo echoJson(-1, $sql . "加入购物车失败！-3");
        }
    }

    /**
     *  删除购物车
     * @param str $uid 账号
     */
    function cancelShoppingCart($uid, $comm_id) {
        $commInfo = strToJsonArr($comm_id);
        if (count($commInfo) < 1) {
            echo echoJson(-1, "删除购物车失败,-2！");
            return;
        }
        $this->mysql->beginTransactionSQL();
        for ($i = 0, $len = count($commInfo); $i < $len; $i++) {
            $comm_id = $commInfo[$i]['comm_id'];
            $comm_sort = $commInfo[$i]['comm_sort'];
            $sql = "DELETE FROM sopp_shoppingCart WHERE uid='$uid' AND comm_id='$comm_id' AND comm_sort='$comm_sort'";
            if (empty($comm_id) || empty($comm_sort) || !mysql_query($sql)) {
                $this->mysql->rollbackTransactionSQL();
                echo echoJson(0, "删除失败！");
                return;
            }
        }
        $this->mysql->submitTransactionSQL();
        echo echoJson(1, "删除成功！");
    }

    /**
     *  修改购物车商品的选中属性
     * @param str $uid 账号
     * @param str $comm_id 商品id
     */
    function editAttribute($uid, $comm_id, $comm_sort) {
        $selected_attribute = $_GET["selected_attribute"];
        $comm_quantity = $_GET["comm_quantity"];
        if (!empty($selected_attribute) && !empty($selected_attribute)) {
            $sql = "UPDATE sopp_shoppingCart SET selected_attribute='$selected_attribute',comm_quantity='$comm_quantity' WHERE comm_id='$comm_id' AND uid='$uid' AND comm_sort='$comm_sort'";
        } else if (empty($selected_attribute) && !empty($selected_attribute)) {
            $sql = "UPDATE sopp_shoppingCart SET comm_quantity='$comm_quantity' WHERE comm_id='$comm_id' AND uid='$uid' AND comm_sort='$comm_sort'";
        } else if (!empty($selected_attribute) && empty($selected_attribute)) {
            $sql = "UPDATE sopp_shoppingCart SET selected_attribute='$selected_attribute' WHERE comm_id='$comm_id' AND uid='$uid' AND comm_sort='$comm_sort'";
        }
        if ($this->mysql->updateSQL($sql)) {
            echo echoJson(1, "修改成功！");
        } else {
            echo echoJson(-1, "修改失败！");
        }
    }

    /**
     *  查询用户的购物车
     * @param str $uid 账号
     */
    function selectShoppingCart($uid) {
        $sql = "SELECT comm_id,comm_sort,datetime,comm_quantity,selected_attribute FROM sopp_shoppingcart  WHERE uid='$uid' ORDER BY datetime DESC";
        $result = $this->mysql->selectSQLAll($sql);
        $jsonArray = array();
        if (is_array($result)) {
            foreach ($result as $row) {
                $comm_sort = !empty($row['comm_sort']) ? $row['comm_sort'] : "";
                $comm_id = !empty($row['comm_id']) ? $row['comm_id'] : false;
                if (($comm_sort == 'all' || $comm_sort == 'home' || $comm_sort == 'consumption' || $comm_sort == 'integral' || $comm_sort == 'activity' || $comm_sort == 'merchant') && $comm_id) {
                    $comm_quantity = (!empty($row['comm_quantity'])) ? $row['comm_quantity'] : 1;
                    $selected_attribute = (!empty($row['selected_attribute'])) ? strToJsonArr($row['selected_attribute']) : [];
                    $selected_attribute_str = $this->getAttributeStr($selected_attribute);
                    $arr = $this->getCommDetails($comm_sort, $comm_id, false);
                    $arr["comm_quantity"] = (int)$comm_quantity;
                    $arr["selected_attribute_str"] = $selected_attribute_str;
                    $arr['comm_attribute'] = $this->getAttribute($comm_id, $selected_attribute);
                    $jsonArray[] = $arr;
                }
            }
            if (count($jsonArray)) {
                echo echoJson(1, $jsonArray);
            } else {
                echo echoJson(-1, "商品空空如也!");
            }
        } else {
            echo echoJson(-1, "商品空空如也!");
        }
    }
}